package com.wyp.controller.student;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wyp.service.impl.ScoresServiceImpl;
import com.wyp.service.impl.TestpapersServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/student")
public class NotfinishExamController {

    @Autowired
    private TestpapersServiceImpl testpapersService;

    @Autowired
    private ScoresServiceImpl scoresService;

    @RequestMapping("/notfinishexam.html")
    public String tologin(Model model,
                          @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                          @RequestParam(value = "pageSize", defaultValue = "4") int pageSize,
                          HttpSession session
    ) {
        Map<String, Object> examinee = (Map<String, Object>) session.getAttribute("examinee");
        if (pageNum <= 0) {
            pageNum = 1;
        }
        //获取该考生已取得成绩的试卷
        List<Integer> t = new ArrayList<>();
        List<Map<String, Object>> scores = scoresService.qureyScoresByStuid((int)examinee.get("stuid"));
        for (Map<String, Object> score : scores) {
            if (score.get("reachtime") != null && !score.get("reachtime").equals("")) {
                t.add(Integer.parseInt(score.get("testpaperid").toString()));
            }
        }
        model.addAttribute("t", t);
        //1.引入分页插件,pageNum是第几页，pageSize是每页显示多少条,默认查询总数count
        PageHelper.startPage(pageNum, pageSize);
        //2.紧跟的查询就是一个分页查询-必须紧跟.后面的其他查询不会被分页，除非再次调用PageHelper.startPage
        try {
            List<Map<String, Object>> testpapersList = testpapersService.qureyTestpapersByAccess((int)examinee.get("apartmentid"));
            //3.使用PageInfo包装查询后的结果,5是连续显示的条数,结果list类型是Page<E>
            PageInfo<Map<String, Object>> pageInfo = new PageInfo<Map<String, Object>>(testpapersList, pageSize);
            //4.使用model/map/modelandview等带回前端
            model.addAttribute("pageInfo", pageInfo);
        } finally {
            PageHelper.clearPage(); //清理 ThreadLocal 存储的分页参数,保证线程安全
        }
        return "student/notfinishexam";
    }

    @RequestMapping("/notfinishexam_qurey.html")
    public String toqureytestlibinfo(
            @Nullable @RequestParam("qureymessage") String qureymessage
    ) throws UnsupportedEncodingException {
        if (qureymessage != null && !(qureymessage.equals(""))) {
            qureymessage = new String(qureymessage.getBytes("utf-8"), "iso-8859-1");
            return "redirect:/student/notfinishexam_qurey.html/" + qureymessage;
        } else {
            return "redirect:/student/notfinishexam.html";
        }
    }

    @RequestMapping("/notfinishexam_qurey.html/{qureymessage}")
    public String qureynofinis(Model model,
                               @Nullable @PathVariable("qureymessage") String qureymessage,
                               @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                               @RequestParam(value = "pageSize", defaultValue = "4") int pageSize,
                               HttpSession session
    ) {
        Map<String, Object> examinee = (Map<String, Object>) session.getAttribute("examinee");
        if (pageNum <= 0) {
            pageNum = 1;
        }
        //获取该考生已取得成绩的试卷
        List<Integer> t = new ArrayList<>();
        List<Map<String, Object>> scores = scoresService.qureyScoresByStuid((int)examinee.get("stuid"));
        for (Map<String, Object> score : scores) {
            if (score.get("reachtime") != null && !score.get("reachtime").equals("")) {
                t.add(Integer.parseInt(score.get("testpaperid").toString()));
            }
        }
        model.addAttribute("t", t);
        //1.引入分页插件,pageNum是第几页，pageSize是每页显示多少条,默认查询总数count
        PageHelper.startPage(pageNum, pageSize);
        try {
            List<Map<String, Object>> testpapersList = testpapersService.qureyTestpapersByApartmentnameAccess((int)examinee.get("apartmentid"), qureymessage);
            //3.使用PageInfo包装查询后的结果,5是连续显示的条数,结果list类型是Page<E>
            PageInfo<Map<String, Object>> pageInfo = new PageInfo<Map<String, Object>>(testpapersList, pageSize);
            //4.使用model/map/modelandview等带回前端
            model.addAttribute("pageInfo", pageInfo);
        } finally {
            PageHelper.clearPage(); //清理 ThreadLocal 存储的分页参数,保证线程安全
        }
        return "student/notfinishexam";
    }

}
